<?php
namespace Home\Controller;
use User\Api\UserApi;

/**
 * 用户控制器
 * 包括用户中心，用户登录及注册
 */
class UserController extends HomeController {
	// public function _initialize(){
 //      if(get_username() == null){
 //        $this->error('请先登录',U('Index/index'));
 //      }
 //    }

	/* 用户中心首页 */
	public function index(){
		if ( !is_login() ) {
			$this->error( '您还没有登陆',U('User/login') );
		}
		$this->display();
	}

	/* 注册页面 */
	public function register($username = '', $password = '', $repassword = '', $email = '', $verify = ''){
		// var_dump($email);
        if(!C('USER_ALLOW_REGISTER')){
            $this->error('注册已关闭');
        }
		if(IS_POST){ //注册用户
			/* 检测验证码 */
			$verify = I('post.code');
			var_dump($_POST,$verify);die;
			if(!check_verify($verify)){
				$this->error('验证码输入错误！');
			}

			/* 检测密码 */
			if($password != $repassword){
				$this->error('密码和重复密码不一致！');
			}			

			/* 调用注册接口注册用户 */
            $User = new UserApi;

            // var_dump($_POST);
            $username = I('post.username');
            $password = I('post.password');
			$uid = $User->register($username,$password, $email);
			// var_dump($uid,$username, $password, $email);die;
			if(0 < $uid){ //注册成功
				//TODO: 发送验证邮件
				$this->success('注册成功！',U('login'));
			} else { //注册失败，显示错误信息
				$this->error($this->showRegError($uid));
			}

		} else { //显示注册表单
			$this->display();
		}
	}

	/* 登录页面 */
	public function login($username = '', $password = '', $verify = ''){
		if(IS_POST){ //登录验证
			/* 检测验证码 */
			// if(!check_verify($verify)){
			// 	$this->error('验证码输入错误！');
			// }

			/* 调用UC登录接口登录 */
			// var_dump($_POST);
			$user = new UserApi;
			$uid = $user->login($username, $password);
			// var_dump($uid);die;
			if(0 < $uid){ //UC登录成功
				/* 登录用户 */
				$Member = D('Member');
				if($Member->login($uid)){ //登录用户
					//TODO:跳转到登录前页面
					$this->success('登录成功！',U('Home/Index/index'));
				} else {
					$this->error($Member->getError());
				}

			} else { //登录失败
				switch($uid) {
					case -1: $error = '用户不存在或被禁用！'; break; //系统级别禁用
					case -2: $error = '密码错误！'; break;
					default: $error = '未知错误！'; break; // 0-接口参数错误（调试阶段使用）
				}
				$this->error($error);
			}

		} else { //显示登录表单
			$this->display();
		}
	}

	/* 退出登录 */
	public function logout(){
		if(is_login()){
			D('Member')->logout();
			$this->success('退出成功！', U('User/login'));
		} else {
			$this->redirect('User/login');
		}
	}

	/* 验证码，用于登录和注册 */
	public function verify(){
		$config =    array(
    'fontSize'    =>    30,    // 验证码字体大小
    'length'      =>    1,     // 验证码位数
    'useNoise'    =>    false, // 关闭验证码杂点
    'imageW'	=>'80' ,        //验证码宽度 设置为0为自动计算
    'imageH'	=>'40',         //验证码高度 设置为0为自动计算
    );
		$verify = new \Think\Verify($config);
		$verify->entry(1);
	}

	/**
	 * 获取用户注册错误信息
	 * @param  integer $code 错误编码
	 * @return string        错误信息
	 */
	private function showRegError($code = 0){
		switch ($code) {
			case -1:  $error = '用户名长度必须在16个字符以内！'; break;
			case -2:  $error = '用户名被禁止注册！'; break;
			case -3:  $error = '用户名被占用！'; break;
			case -4:  $error = '密码长度必须在6-30个字符之间！'; break;
			case -5:  $error = '邮箱格式不正确！'; break;
			case -6:  $error = '邮箱长度必须在1-32个字符之间！'; break;
			case -7:  $error = '邮箱被禁止注册！'; break;
			case -8:  $error = '邮箱被占用！'; break;
			case -9:  $error = '手机格式不正确！'; break;
			case -10: $error = '手机被禁止注册！'; break;
			case -11: $error = '手机号被占用！'; break;
			default:  $error = '未知错误';
		}
		return $error;
	}


    /**
     * 修改密码提交
     * @author huajie <banhuajie@163.com>
     */
    public function profile(){
		if ( !is_login() ) {
			$this->error( '您还没有登陆',U('User/login') );
		}
        if ( IS_POST ) {
            //获取参数
            $uid        =   is_login();
            $password   =   I('post.old');
            $repassword = I('post.repassword');
            $data['password'] = I('post.password');
            empty($password) && $this->error('请输入原密码');
            empty($data['password']) && $this->error('请输入新密码');
            empty($repassword) && $this->error('请输入确认密码');

            if($data['password'] !== $repassword){
                $this->error('您输入的新密码与确认密码不一致');
            }
            // var_dump($uid,$password,$repassword,$data);
            $Api = new UserApi();
            // var_dump($Api);die;
            $res = $Api->updateInfo($uid, $password, $data);
            if($res['status']){
                $this->success('修改密码成功！');
            }else{
                $this->error($res['info']);
            }
        }else{
            $this->display('User/userprofile');
        }
    }


    public function usercenter(){
    	$map['status']=array('egt',0);
    	$map['username'] =get_username();
    	$order = $this->page($map);
    	$this->display();
    }
    public function newPath(){
    	
    	$this->display();
    }
    public function usersub(){
    	
    	$this->display();
    }
     public function usertui(){
    	
    	$this->display();
    }


    //订单其他分类
    public function other(){
       $tatus = I('get.status');
       $map = array('status'=>$tatus,'username'=>get_username());
       $order = $this->page($map);
       // var_dump($order);
       $this->display('usercenter');
    }


    public function page($map,$order="ctime",$table="carshop"){
		$good = M($table); 
        // if (!empty($list)) {
        //   $good  =$list;
        //   }  
         
		$count      = $good->where($map)->count();
		$page       = new \Think\Page($count,3);
		    $page->setConfig('prev', '上一页');
            $page->setConfig('next', '下一页');
            $page->setConfig('theme', '%UP_PAGE% %LINK_PAGE% %DOWN_PAGE%');
		    $show  = $page->show();
		     
		$list = $good->where($map)->order($order)->limit($page->firstRow.','.$page->listRows)->select();
		$list = $this->blxq($list);
		// var_dump($list,$map,$show);
		$this->assign('lists',$list);// 赋值数据集
		$this->assign('page',$show);// 赋值分页输出
	}
	//遍历详情
	public function blxq($order){
       foreach ($order as $k => $v) {
    		$map =array('id'=>$v['gid']);
    		$res = M('goodshop')->field('name,pic')->where($map)->find();
    		$order[$k]['good']=$res;
    	}
    	return $order;
	}

}
